**********************************************************************
*Patient level regs - NY data 
*Run on NY server
**********************************************************************
	*File options 
	global is is
	
	*GLOBALS, PACKAGES 
	{
	global tb   "<folder with NY discharge data>"
	global regs "<folder with regression files>"
	global op   "<folder with output>"
	
	ssc install egenmore
	ssc install outreg2 
	ssc install tabout
	ssc install distinct
	set matsize 5000
	set emptycells drop
	capture rm "$op/pregs_cardio${is}.xls"
	capture rm "$op/pdvmean_cardio${is}.xls"
	}
	
	*CONTROL VARS 
	{
	global pc female age inp_util_c elix
	global hc2 bdtot teach mcare mcaid hhi
	global area rural white college unempl poverty elderly state_exp_status 		
	global hospcontrol $hc2 $area
	global ptntcontrol $pc 
	global control never 
	global treat (incl_1${is})	
	global complics p_read90_inp p_read30_inp p_read45_inp p_read60_inp p_read75_inp 
	global drg tot_drgwt
	global fe i.year i.num_prv
	global print keep(bought_1${is})
	
	*Use CEM matching sample throughout 
	global WT "[aw=wt2]"
	}

**********************************************************************
	*I. PREPARE REGRESSION INPUTS
	*Data prep: This file assumes the construction of episode files from New York
	*inpatient discharge data, which has been merged to American Hospital 
	*Association (AHA) IDs and includes readmission and Elixhauser score variables. 
	*The below code constructs the inputs files to all regressions, Tables, and 
	*Figures using NY discharge data.
	
	*LIMIT TO ELEVANCE SAMPLE
	{
	use "${regs}/cardio_ptntny_all_proc.dta", clear
	keep ahaid ye 
	gduplicates drop 
	tempfile hospsample
	save `hospsample', replace	
	
	*Limit to hospitals & deals in Elevance regression sample 
	use num ye bought_1$is incl_1$is wt_cemm hhi using "$tb/server_wts_for_atul/fulldata_wts${is}", clear
	merge 1:m ahaid ye using `hospsample'
	bysort ahaid: gegen has_m = max(_m)
	keep if has_m==3 	
	drop if _m==1
	drop _m has_m
	
	drop if ye < 2012
	sort ahaid ye 
	foreach var in bought_1$is incl_1$is wt_cemm hhi {
	replace `var' = `var'[_n-1] if missing(`var') & ahaid==ahaid[_n-1] 
	forval i = 1/10 {
	replace `var' = `var'[_n+1] if missing(`var') & ahaid==ahaid[_n+1] 
	}
	}
	count if missing(bought_1$is) 
	assert r(N)==0 
	gen never = !incl_1$is 
	
	*Event study vars 
	gen boughtyr1 = year if bought_1$is ==1 & incl_1$is ==1
	bysort ahaid: gegen boughtyr = min(boughtyr1)
	forval i = 0/4 {
	gen pre`i'_1$is    = (ye==boughtyr-`i')
	gen post`i'_1$is   = (ye==boughtyr+`i')
	}
	drop post0* boughtyr*
	qui compress 
	sort ahaid ye 
	tempfile dd
	save `dd', replace 	
	
	*Merge NY & Elevance samples 
	use "${regs}/cardio_ptntny_all_proc", clear
	keep key visitlink daystoevent year ahaid los mcare_flg commercial died mdnum* $hospcontrol $ptntcontrol sysid bought_$is drg tot_drgwt p_read??_inp inp_util_c elix* 
 	sort ahaid ye 
	merge m:1 ahaid ye using `dd', sorted
	keep if _m==3
	drop _m num 
	gegen num_prv = group(ahaid)
	
	*Matching weights 
	gen wt2 = wt_cemm > 0 
	
	*Flag Medicare Advantage  
	sort key
	merge 1:1 key using "$tb/ma_flag", sorted
	drop if _m==2
	drop _m 
	}
	
	*LIMIT TO TREATED, CONTROL GROUPS
	gen t3samp${is} = (pre3_1${is}==1 | pre2_1${is}==1 | pre1_1${is}==1 | pre0_1${is}==1 | post1_1${is} ==1 | post2_1${is} ==1 | post3_1${is}==1 | never==1)
	keep if t3samp${is} | never 
	drop if missing(tot_drgwt) | missing(p_read90_inp)
	
	
**********************************************************************
	*II. ANALYSIS 
	*The below code constructs the Tables and Figures that use NY discharge data.
	*1. NY quality 
	*Tables 6, A9
	*Figures 7, A7
	foreach var in p_read90_inp died { 
	
	*DD
	qui reg `var' bought_1${is}   $fe $ptntcontrol $hospcontrol $drg if ($treat | $control) $WT , vce(cluster num_prv) 
	outreg2 using "$op/pregs_cardio${is}.xls", append $print nocons ctitle("${is}_DW_`var'") 
	
	*Event study 
	reg `var' pre3_1${is} pre2_1${is} pre0_1${is} post1_1${is} post2_1${is} post3_1${is} $hospcontrol $ptntcontrol $drg $fe if ($treat | $control) $WT , vce(cluster num_prv) 
	outreg2 using "$op/pregs_cardio${is}.xls", append keep(pre* post*) nocons ctitle("${is}_DW_`var'") 	
	
	*DV mean & pretrends
	test pre2_1${is} pre3_1${is}
	gen pre = round(r(p),.01)
	qui tabout pre1_1${is}  if ($treat | $control) $WT using "$op/pdvmean_cardio${is}.xls", sum cells(mean `var' N `var' mean pre) f(4 0 2) total("`var'_${is}") append 
	drop pre 
	}
	
	*2. Readmissions by duration
	*Figure A5
	foreach var in $complics { 
	qui reg `var' bought_1${is}   $fe $ptntcontrol $hospcontrol $drg if ($treat | $control) $WT , vce(cluster num_prv) 
	outreg2 using "$op/pregs_cardio${is}.xls", append $print nocons ctitle("${is}_LRD_`var'") 	
	
	qui tabout pre1_1${is}  if ($treat | $control) $WT using "$op/pdvmean_cardio${is}.xls", sum cells(mean `var' N `var') f(4 0) total("`var'_${is}") append 
	}
	
	*3. Patient mix 
	*Tables A3, A7
	{
	preserve 
	drop if missing(age) 
	qui sum los, d
	replace los = r(mean) if missing(los)
	
	foreach var in tot_drgwt age los elix {
	
	qui reg `var' bought_1${is}           $fe if ($treat | $control), vce(cluster num_prv) 
	outreg2 using "$op/pregs_cardio${is}.xls", append $print nocons ctitle("${is}_noc_`var'") 
	
	qui reg `var' bought_1${is} tot_drgwt $fe if ($treat | $control), vce(cluster num_prv) 
	outreg2 using "$op/pregs_cardio${is}.xls", append $print nocons ctitle("${is}_DW_`var'") 
	
	tabout pre1_1${is}  if ($treat | $control) using "$op/pdvmean_cardio${is}.xls", sum cells(mean `var' N `var') f(4 0) total("${is}_`var'") append 
	}
	restore
	}
	
	*4. Cardiac volume 
	*Table A4
	{
	gcollapse (count) key (mean) wt* bought_1${is} ${treat} ${control} pre*${is} post*${is}* $hospcontrol, by(ahaid ye)	
	gen vol 	    = key 
	gen vol_bed 	= key / bdtot
	local vol *vol* 
	unab vol: `vol'
	
	destring ahaid, replace 
	xtset ahaid year
	foreach var in `vol' { 
	global fe i.year
		
	qui xtreg `var' bought_1${is} $hospcontrol $fe if (${treat} | ${control}) $WT , fe vce(cluster ahaid) 
	outreg2 using "$op/pregs_cardio${is}.xls", append $print nocons ctitle("${is}_c_`var'") 
	
	xtreg `var' pre3_1${is} pre2_1${is} pre0_1${is} post1_1${is} post2_1${is} post3_1${is} $hospcontrol $fe if (${treat} | ${control}) $WT , fe vce(cluster ahaid) 
	outreg2 using "$op/pregs_cardio${is}.xls", keep(pre* post*) nocons ctitle("${is}_c_`var'")
	
	qui tabout pre1_1${is}  if (${treat} | ${control}) $WT using "$op/pdvmean_cardio${is}.xls", sum cells(mean `var' N `var') f(4 0) total("`var'") append 
	}
	}